package de.schule.barnimgymnasium.autotransporter;

import java.util.ArrayList;

import org.eclipse.swt.SWT;
import org.eclipse.swt.SWTException;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.events.MouseMoveListener;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.layout.RowData;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Composite;

import de.schule.barnimgymnasium.autotransporter.spiele.SpielEventListener;

public class GameArea extends Canvas {
	public static final String fotograf = "TimsUndAlexAutobilder";
	ArrayList<GameItem> items = new ArrayList<GameItem>(0);
	ArrayList<SnapPoint> snaps = new ArrayList<SnapPoint>(0);
	GameItem movedItem = null;

	public GameArea(Composite parent, int anzahlTransporter) {
		super(parent, SWT.None);
		try {
			final Image img = new Image(getDisplay(), "./bilder/" + fotograf
					+ "/background.jpg");
			setBackgroundImage(img);
			setLayoutData(new RowData(img.getBounds().width,
					img.getBounds().height));
			setBounds(0, 0, img.getBounds().width, img.getBounds().height);
		} catch (SWTException e) {
			setLayoutData(new RowData(800, 600));
			setBounds(0, 0, 800, 600);
		}
		// final Image pic = new Image(getDisplay(), "./pic.png");
		for (int nummer = 1; nummer <= anzahlTransporter; nummer++) {
			final Transporter transporter = new Transporter(this, 10,
					100 * nummer, nummer);
			this.snaps.addAll(transporter.getSnapPoints());
			this.items.add(transporter);

			final Auto auto1 = new Auto(this, 350, 120 * nummer, nummer,
					new RGB(255, 255, 0));
			auto1.setImage(new Image(getDisplay(), "./bilder/" + fotograf
					+ "/gelbesAuto.png"));
			auto1.setGeraeusch(this.getClass().getResource("gelbesAuto.wav"));
			this.items.add(auto1);

			final Auto auto2 = new Auto(this, 500, 120 * nummer, nummer,
					new RGB(255, 0, 0));
			auto2.setImage(new Image(getDisplay(), "./bilder/" + fotograf
					+ "/rotesAuto.png"));
			auto2.setGeraeusch(this.getClass().getResource("rotesAuto.wav"));
			this.items.add(auto2);
			final Auto auto3 = new Auto(this, 350, 120 * nummer + 60, nummer,
					new RGB(0, 255, 0));
			auto3.setImage(new Image(getDisplay(), "./bilder/" + fotograf
					+ "/gruenesAuto.png"));
			auto3.setGeraeusch(this.getClass().getResource("gruenesAuto.wav"));
			this.items.add(auto3);

			final Auto auto4 = new Auto(this, 500, 120 * nummer + 60, nummer,
					new RGB(0, 0, 255));
			auto4.setImage(new Image(getDisplay(), "./bilder/" + fotograf
					+ "/blauesAuto.png"));
			auto4.setGeraeusch(this.getClass().getResource("blauesAuto.wav"));
			this.items.add(auto4);
		}
		addPaintListener(new PaintListener() {

			@Override
			public void paintControl(final PaintEvent event) {
				// Malen in umgekehrter Reihenfolge, erst hinten, dann vorn.
				for (int i = items.size() - 1; i >= 0; i--) {
					final GameItem item = items.get(i);
					item.paintControl(event);
				}
			}
		});

		addMouseListener(new MouseListener() {

			@Override
			public void mouseUp(final MouseEvent e) {
				if (movedItem == null)
					return;
				if (movedItem.mouseUp(e)) {
					// Item wird nicht mehr bewegt, eventuell auf Stellplatz
					// stellen
					if (movedItem instanceof Auto) {
						for (GameItem item : items) {
							if (item instanceof Transporter) {
								Transporter transporter = (Transporter) item;
								for (Stellplatz s : transporter
										.getStellplaetze()) {
									try {
										s.platziereAuto((Auto) movedItem);
									} catch (NotEmptyException e1) {

									}
								}
							}
						}
					}
					/*
					 * for (final SnapPoint snap : Programm.this.snaps) { if
					 * (snap.location.x == item.getLocation().x &&
					 * snap.location.y == item.getLocation().y) {
					 * item.setLocation(snap.location);
					 * 
					 * } }
					 */
					movedItem = null;

				}

			}

			@Override
			public void mouseDown(final MouseEvent e) {
				for (final GameItem item : items) {
					if (item.mouseDown(e)) {
						movedItem = item;
						// Item soll bewegt werden, Schnapper entfernen
						// for (final SnapPoint snap : snaps) {
						// if (snap.location == item.getLocation()) {
						// item.setLocation(new Point(
						// item.getLocation().x, item
						// .getLocation().y));
						// }
						// }
						break;
					}
				}
			}

			@Override
			public void mouseDoubleClick(final MouseEvent e) {
			}
		});

		addMouseMoveListener(new MouseMoveListener() {

			@Override
			public void mouseMove(final MouseEvent e) {
				if (movedItem == null)
					return;
				if (movedItem.mouseMove(e)) {
					// Das Item wurde bewegt, vielleicht zu einem Schnapper?
					for (final SnapPoint snap : snaps) {
						if (snap.nearby(movedItem)) {
							movedItem.movetoSnap(snap.getLocation());
						}
					}
				}
				redraw();
			}
		});
	}

	public SpielEventListener getSpielEventListener(int i) {
		for (GameItem item : items) {
			if (item instanceof Transporter) {
				Transporter t = (Transporter) item;
				if (t.getNummer() == i) {
					return t.getSpielEventListener();
				}
			}
		}
		return null;
	}
}
